package com.muyu.cloud.system.controller;

import com.muyu.cloud.system.domain.BookInfo;
import com.muyu.cloud.system.domain.request.BookInfoSaveReq;
import com.muyu.cloud.system.service.BookInfoService;
import com.muyu.common.result.Result;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: DongZeLiang
 * @date: 2024/5/23
 * @Description: 书籍控制层
 * @Version: 1.0
 */
@RestController
@AllArgsConstructor
@RequestMapping("/book-info")
public class BookInfoController {

    /**
     * 书籍业务层
     */
    private final BookInfoService bookInfoService;

    /**
     * 添加数据
     * @param bookInfoSaveReq 数据对象
     */
    @PostMapping
    public Result<String> save (@RequestBody @Validated BookInfoSaveReq bookInfoSaveReq) {
        bookInfoService.save(BookInfo.saveReqBuild(bookInfoSaveReq));
        return Result.success();
    }

    /**
     * 通过书籍ID查询书籍信息
     *
     * @param bookId 书籍ID
     *
     * @return 数据信息
     */
    @GetMapping("/{bookId}")
    public Result<BookInfo> selectByBookId (@PathVariable("bookId") Integer bookId) {
        BookInfo bookInfo = bookInfoService.selectByBookId(bookId);
        return Result.success(bookInfo);
    }

    /**
     * 通过书籍信息查询集合
     *
     * @param bookInfo 数据信息
     *
     * @return 符合集合内容
     */
    @PostMapping("/list")
    public Result<List<BookInfo>> list (@RequestBody BookInfo bookInfo) {
        List<BookInfo> bookInfoList = bookInfoService.list(bookInfo);
        return Result.success(bookInfoList);
    }

    /**
     * 通过ID修改书籍信息
     *
     * @param bookInfo 数据信息
     *
     * @return 修改条数
     */
    @PutMapping()
    public Result<String> updateByBookId (BookInfo bookInfo) {
        bookInfoService.updateByBookId(bookInfo);
        return Result.success();
    }

    /**
     * 通过书籍ID删除书籍信息
     *
     * @param bookId 书籍ID
     *
     * @return 返回删除条数
     */
    @DeleteMapping("/{bookId}")
    public Result<String> deleteByBookId (@PathVariable("bookId") Integer bookId) {
        bookInfoService.deleteByBookId(bookId);
        System.out.println("把花都阿斯");
        System.out.println("把花都阿斯");
        System.out.println("把花都阿斯");
        System.out.println("把花都阿斯");
        System.out.println("把花都阿斯");
        System.out.println("把花都阿斯");
        return Result.success();
    }

}
